/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.admin;

import com.max.oti.system.model.State;
import com.max.oti.system.service.interfac.IStateService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author Derlinn
 */
@ManagedBean
@ViewScoped
public class StateBean implements Serializable {

   
    @ManagedProperty("#{stateService}")
    IStateService stateService;
    
    List<State> listState = null;
    List<State> listStateFilter;
    private List<String> nameEntitys;
    private SelectItem[] nameEntitysFilter;
    
    private State state = new State();
    
    @PostConstruct
    public void initBean() {
        nameEntitys = new ArrayList<String>();
        nameEntitys.add("User");
        nameEntitys.add("AlumnRequirement");
        nameEntitys.add("Comission");
        nameEntitys.add("ExamSufficiency");
        nameEntitys.add("HistoryPractice");
        nameEntitys.add("HistoryTitle");
        nameEntitys.add("Member1");
        nameEntitys.add("PlaningTesis");
        nameEntitys.add("ReportPractice");
        nameEntitys.add("Sustentation");
        nameEntitys.add("TypeComission");
        
        nameEntitysFilter = new SelectItem[11];
        nameEntitysFilter[0] = new SelectItem("User", "User");
        nameEntitysFilter[1] = new SelectItem("AlumnRequirement", "AlumnRequirement");
        nameEntitysFilter[2] = new SelectItem("Comission", "Comission");
        nameEntitysFilter[3] = new SelectItem("ExamSufficiency", "ExamSufficiency");
        nameEntitysFilter[4] = new SelectItem("HistoryPractice", "HistoryPractice");
        nameEntitysFilter[5] = new SelectItem("HistoryTitle", "HistoryTitle");
        nameEntitysFilter[6] = new SelectItem("Member1", "Member1");
        nameEntitysFilter[7] = new SelectItem("PlaningTesis", "PlaningTesis");
        nameEntitysFilter[8] = new SelectItem("ReportPractice", "ReportPractice");
        nameEntitysFilter[9] = new SelectItem("Sustentation", "Sustentation");
        nameEntitysFilter[10] = new SelectItem("TypeComission", "TypeComission");
        
        System.out.println("Construido....");
    }
    
    public void saveState(){
        System.out.println("id: " + state.getId());
        System.out.println("lista: "+ listState.size());
        if(state.getId() == null){
            try {
                stateService.save(state);
                FacesUtil.addMessage("Regsitro con Exito", null, FacesMessage.SEVERITY_INFO);
                listState.add(state);
                state = new State();
            } catch (Exception ex) {
                Logger.getLogger(StateBean.class.getName()).log(Level.SEVERE, null, ex);
                FacesUtil.addMessage("Error en el Registro", null, FacesMessage.SEVERITY_FATAL);
            }
        }else{
            try {
                stateService.update(state);
                FacesUtil.addMessage("Modificado con Exito", null, FacesMessage.SEVERITY_INFO);
                state = new State();
                listState = stateService.listHQL("FROM State s");
            } catch (Exception ex) {
                Logger.getLogger(StateBean.class.getName()).log(Level.SEVERE, null, ex);
                FacesUtil.addMessage("Error en la Modificacion", null, FacesMessage.SEVERITY_INFO);
            }
        }
    }
    
    public void deleteState(){
        try {
            stateService.delete(state);
            listState = stateService.listHQL("FROM State s");
            state = new State();
            FacesUtil.addMessage("Eliminado con Exito", null, FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            FacesUtil.addMessage("No Se Puede Eliminar", "Ya esta siendo usado", FacesMessage.SEVERITY_WARN);
        }
    }
    
    public List<State> getListState() {
        if (listState == null) {
            try {
                listState = stateService.listHQL("FROM State s");
            } catch (Exception ex) {
                Logger.getLogger(StateBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return listState;
    }

    public void setListState(List<State> listState) {
        this.listState = listState;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public IStateService getStateService() {
        return stateService;
    }

    public void setStateService(IStateService stateService) {
        this.stateService = stateService;
    }

    public List<String> getNameEntitys() {
        return nameEntitys;
    }

    public List<State> getListStateFilter() {
        return listStateFilter;
    }

    public void setListStateFilter(List<State> listStateFilter) {
        this.listStateFilter = listStateFilter;
    }

    public SelectItem[] getNameEntitysFilter() {
        return nameEntitysFilter;
    }

    public void setNameEntitysFilter(SelectItem[] nameEntitysFilter) {
        this.nameEntitysFilter = nameEntitysFilter;
    }
    
    
}
